﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxMenu;
using System.Xml;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses;

namespace SGDemo.Permission
{
    public partial class ButtonItem : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");
        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);

            BindComBoBox();
        }
        //模块的绑定下拉框
    
        public void BindComBoBox()
        {
            List<ModuleEntity> moduleList = _permissionService.Module_GetAll();
            foreach (ModuleEntity list in moduleList)
            {
                ListEditItem listbox = new ListEditItem();
                listbox.Text = list.ModuleName;
               
                listbox.Value = list.Keyid;
                cbModule.Items.Add(listbox);

            }
        }

        public void cbPage_Callback(object sender, CallbackEventArgsBase  e)
        {
            var keyid = e.Parameter;
           List<PageEntity> pagelist= _permissionService.Page_GetByModuleKeyid(keyid);
           foreach (PageEntity list in pagelist)
           {
               ListEditItem listbox = new ListEditItem();
               listbox.Text = list.PageName;
               listbox.Value = list.Keyid;
               cbPage.Items.Add(listbox);
           }
       
        }
        /// <summary>
        /// 菜单项绑定事件 - 实现操作权限控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void menuHeader_ItemDataBound(object sender, MenuItemEventArgs e)
        {
            IHierarchyData itemHierarchyData = (IHierarchyData)e.Item.DataItem;
            XmlElement element = (XmlElement)itemHierarchyData.Item;

        }

        /// <summary>
        /// 新增保存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveForAdd(ButtonEntity button)
        {
            //加入Session等服务端信息
            button.CreateName = ApplicationContext.Current.RealName;
            button.CreateDate = System.DateTime.Now;
            return _permissionService.Button_Add(button);
        }

        /// <summary>
        /// 获取一个按钮信息
        /// </summary>
        /// <param name="keyid">用户Keyid</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public ButtonEntity GetButtonByKeyid(string keyid)
        {

            return _permissionService.Button_GetByKeyid(keyid);
        }

        /// <summary>
        /// 编辑保存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveForEdit(ButtonEntity button)
        {
            //加入Session等服务端信息
            button.ModifyName = ApplicationContext.Current.RealName;
            button.ModifyDate = System.DateTime.Now;
            bool ret = _permissionService.Button_Update(button);
            return ret;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool Delete(string keyid)
        {
            return _permissionService.Button_Delete(keyid);
        }
    }
}